const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});


rl.question('输入M和N： ', (name) => {
  let [m, n] = name.split(' ').map(Number);
  rl.question('输入占据空间数组： ', (space) => {
    let zhiArr = space.split(' ').map(Number);
    rl.question('输入价值数组： ', (values) => {
      let valArr = values.split(' ').map(Number);

      const dp = new Array(m).fill(0).map(() => new Array(n + 1).fill(0));

      for (let j = zhiArr[0]; j <= n; j++) {
        dp[0][j] = valArr[0];
      }
      for (let i = 1; i < m; i++) {
        for (let j = 1; j <= n; j++) {
          if (zhiArr[i] > j) {
            dp[i][j] = dp[i - 1][j];
          } else {
            dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - zhiArr[i]] + valArr[i]);
          }
        }
      }
      console.log(dp[m - 1][n]);
      rl.close();
    })
  })
});



//  一次性监听输入
function maxValue(m, n, zhiArr, valArr) {
  const dp = new Array(m).fill(0).map(() => new Array(n + 1).fill(0));
      for (let j = zhiArr[0]; j <= n; j++) {
        dp[0][j] = valArr[0];
      }
      for (let i = 1; i < m; i++) {
        for (let j = 1; j <= n; j++) {
          if (zhiArr[i] > j) {
            dp[i][j] = dp[i - 1][j];
          } else {
            dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - zhiArr[i]] + valArr[i]);
          }
        }
      }
      console.log(dp[m - 1][n]);
}

let input = [];

rl.on('line', (line) => {
  input.push(line);
})

rl.on('close', () => {
  let [m, n] = input[0].split(' ').map(Number);
  let zhiArr = input[1].split(' ').map(Number);
  let valArr = input[2].split(' ').map(Number);
  maxValue(m, n, zhiArr, valArr);
})